/**
 * 作者:hanbo
 * 日期:2018/11/2111:54
 * 描述:
 */
import 'jquery';
import 'jquery-browserify';
import 'jquery-mousewheel';

export class InteractiveEvent {
    /**
     * 构造器
     * @param mapView [MapView] 地图控件
     */
    constructor(mapView) {
        this._mapView = mapView;
        this._mouseDown = false;
        this._moved = false;
    }

    init(id) {
        const that = this;
        //注册canvas事件
        let canvas = $("#" + id + " canvas");
        //鼠标滚轮事件


        this.isZoom = false;

        canvas.bind("mousewheel", function (e) {
            let delta = e.originalEvent.deltaY * 0.0001;
            that.zoomDelta(delta);
            //缩放结束事件检测
            if (that.clock) {
                clearTimeout(that.clock);
            }
            that.clock = setTimeout(function () {
                that._mapView._eventCenter.onZoomEnd();
            }, 250);

        });
        // canvas.click(this.click);
        // canvas.dblclick(this.dbClick);
        // // canvas.mouseenter(this.mouseEnter);
        canvas.mouseleave(function (e) {
            that.mouseLeave(e.screenX, e.screenY);
        });
        canvas.mousedown(function (e) {
            that.mouseDown(e.screenX, e.screenY);
        });
        canvas.mouseup(function (e) {
            that.mouseUp(e.screenX, e.screenY);
        });
        canvas.mousemove(function (e) {
            that.mouseMove(e.screenX, e.screenY);
        });
    }

    /**
     * 点击事件
     * @param e
     */
    click(e) {

    }

    /**
     * 双击事件
     * @param e
     */
    dbClick(e) {

    }

    /**
     * 鼠标进入事件
     * @param e
     */
    mouseEnter(e) {

    }

    /**
     * 鼠标离开事件
     * @param e
     */
    mouseLeave(e) {
        this._mouseDown = false;
        this._checkMoved();
    }

    /**
     * 鼠标按下事件
     */
    mouseDown(screenX, screenY) {
        this._mouseDown = true;
        this._startX = screenX;
        this._startY = screenY;
    }

    /**
     * 鼠标抬起事件
     * @param e
     */
    mouseUp(e) {
        this._mouseDown = false;
        this._checkMoved();
    }

    /**
     * 鼠标移动事件
     * @param e
     */
    mouseMove(screenX, screenY) {
        if (this._mouseDown) {
            this._moved = true;
            //如果鼠标在按下状态，则计算偏移量
            let dx = screenX - this._startX;
            let dy = this._startY - screenY;
            this._startX = screenX;
            this._startY = screenY;
            this._mapView.gLStateCenter.move(dx, dy);
        }
    }

    /**
     * 传递缩放值
     * @param delta
     */
    zoomDelta(delta) {
        this._mapView.gLStateCenter.zoom(delta);
    }

    /**
     * 结束时检测是否移动地图，触发移动结束事件
     * @private
     */
    _checkMoved() {
        if (this._moved) {
            this._moved = false;
            this._mapView._eventCenter.onMoveEnd();
        }
    }


}